/*
 * 会话模块 
 */

'use strict'

YX.fn.session = function() {

	}
	/**
	 * 最近联系人显示
	 * @return {void}
	 */
YX.fn.buildSessions = function(id) {
		var data = {
			sessions: this.cache.getSessions()
		}

		if(!this.sessions) {
			var options = {
				data: data,
				onclickavatar: this.showInfo.bind(this),
				onclickitem: this.openChatBox.bind(this),
				infoprovider: this.infoProvider.bind(this),

			}
			this.sessions = new NIMUIKit.SessionList(options)

			this.sessions.inject($('#sessions').get(0))
		} else {
			this.sessions.update(data)
				//this.teams.update(data)
		}
		//导航上加未读示例  
		this.showUnread()
		this.doPoint()
			//添加@标签
		this.showAltSum()
			//已读回执处理
		this.markMsgRead(id)
	}


	/**
	 * 最近联系人显示
	 * @return {void}
	 */
YX.fn.buildSessionsDoMsg = function(id) {
		var data = {
			sessions: this.cache.getSessions()
		}

		if(!this.sessions) {
			var options = {
				data: data,
				onclickavatar: this.showInfo.bind(this),
				onclickitem: this.openChatBox.bind(this),
				infoprovider: this.infoProvider.bind(this),

			}
			this.sessions = new NIMUIKit.SessionList(options)

			this.sessions.inject($('#sessions').get(0))
		} else {
			this.sessions.update(data)
				//this.teams.update(data)
		}
		//导航上加未读示例  
		this.showUnread()
		this.doPoint()
			//添加@标签
		this.showAltSumDoMsg()
			//已读回执处理
		this.markMsgRead(id)
	}



	// 导航上加未读数
YX.fn.showUnread = function() {

	var counts = $("#sessions .panel_count")
	this.totalUnread = 0
	if(counts.length !== 0) {
		if(this.totalUnread !== "99+") {
			for(var i = counts.length - 1; i >= 0; i--) {
				if($(counts[i]).text() === "99+") {
					this.totalUnread = "99+"
					break
				}
				this.totalUnread += parseInt($(counts[i]).text(), 10)
			}
		}
	}
	var $node = $(".m-unread .u-unread")
	$node.text(this.totalUnread)
	this.totalUnread ? $node.removeClass("hide") : $node.addClass("hide")
}

//zdy 添加@数量
YX.fn.showAltSum = function() {
	var that = this
	var panel_items = $("#sessions .panel_item")
	for(var i = 0; i < panel_items.length; i++) {
		panel_items.eq(i).attr("a-me", "")
		var scene = panel_items.eq(i).attr("data-scene")
		var account = panel_items.eq(i).attr("data-account")
			//获取未读数
		var session = this.cache.findSession(scene + "-" + account);
		if(session.unread > 0) {
			this.updateShowAlt(scene, account, session);
		}

	}

}


//zdy 添加@数量
YX.fn.showAltSumDoMsg = function() {
	var that = this
	var panel_items = $("#sessions .panel_item")
	for(var i = 0; i < panel_items.length; i++) {
		panel_items.eq(i).attr("a-me", "")
		var scene = panel_items.eq(i).attr("data-scene")
		var account = panel_items.eq(i).attr("data-account")
			//获取未读数
		var session = this.cache.findSession(scene + "-" + account);
		if(session.unread > 0) {
			this.updateShowAltDoMsg(scene, account, session);
		}
	}
}

/*****
 * 
 * that.mysdk.getHistoryMsgs获取不到最后一条msg,，需要将最后一条去掉，再在开头加入lastMsg
 */
YX.fn.updateShowAltDoMsg = function(scene, account, session) {
	console.log(session.lastMsg.text)
	var that = this
	var as = 0
	var param = {
		scene: scene,
		to: account,
		reverse: false,
		limit: session.unread,
		done: function(error, obj) {
			if(!error) {
				var msgs = obj.msgs
				msgs.pop()
				msgs.unshift(session.lastMsg)
				
				var li = $("li[data-account='" + account + "']")
				for(var j = 0; j < msgs.length; j++) {
					if(msgs[j].apns) {
						if(null !== msgs[j].apns.accounts) {
							if(msgs[j].apns.accounts.contains(that.accid)) {
								li.attr("a-me", msgs[j].idClient)
								as++
							}
						}
					}
				}
				
				if(as > 0) {
					var panel_item = $("li[data-account='" + session.id.split("-")[1] + "']");
					if(null != panel_item.find(".panel_multi-row .panel_been_a")) {
						panel_item.find(".panel_multi-row .panel_been_a").remove()
					}
					panel_item.find(".panel_multi-row").eq(1).append("<span class=\"panel_been_a\">@" + (as == 1 ? "" : as) + "</span>");
				} else {
					var panel_item = $("li[data-account='" + session.id.split("-")[1] + "']");
					if(null != panel_item.find(".panel_multi-row .panel_been_a")) {
						panel_item.find(".panel_multi-row .panel_been_a").remove()
					}
				}
			}
		}

	}
	that.mysdk.getHistoryMsgs(param)
}

YX.fn.updateShowAlt = function(scene, account, session) {
	console.log(session.lastMsg.text)
	var that = this
	var as = 0
	var param = {
		scene: scene,
		to: account,
		reverse: false,
		limit: session.unread,
		done: function(error, obj) {
			if(!error) {
				var msgs = obj.msgs
				var li = $("li[data-account='" + account + "']")
				for(var j = 0; j < msgs.length; j++) {
					console.log("--------------" + j)
					console.log(msgs[j])
					if(msgs[j].apns) {
						if(null !== msgs[j].apns.accounts) {
							if(msgs[j].apns.accounts.contains(that.accid)) {
								li.attr("a-me", msgs[j].idClient)
									//console.log(msgs[j].text)
								as++
							}
						}

					}
				}
				if(as > 0) {
					var panel_item = $("li[data-account='" + session.id.split("-")[1] + "']");
					if(null != panel_item.find(".panel_multi-row .panel_been_a")) {
						panel_item.find(".panel_multi-row .panel_been_a").remove()
					}
					panel_item.find(".panel_multi-row").eq(1).append("<span class=\"panel_been_a\">@" + (as == 1 ? "" : as) + "</span>");
				} else {
					var panel_item = $("li[data-account='" + session.id.split("-")[1] + "']");
					if(null != panel_item.find(".panel_multi-row .panel_been_a")) {
						panel_item.find(".panel_multi-row .panel_been_a").remove()
					}
				}
			}
		}

	}
	that.mysdk.getHistoryMsgs(param)
}

//zdy 添加@数量
/*YX.fn.zdyTeamshowUnreadSum = function() {
	var that = this
	var panel_items = $("#teams .panel_item")

	for(var i = 0; i < panel_items.length; i++) {


		var type = panel_items.eq(i).attr("data-type") 
		var account = panel_items.eq(i).attr("data-account")
		//获取未读数
		var session = this.cache.findSession(type + "-" + account);

		if(session.unread > 0) {
			//this.updateShowAlt(scene, account, session);
			panel_items.eq(i).find(".panel_avatar").append("<b class='zdy-panel_count'>"+session.unread+"</b>")
		}

	}

}*/

//群组未读信息处理
/*YX.fn.zdyupdateTeamShowAlt = function(scene, account, session) {
	var that = this
	var as = 0
	var param = {
		scene: scene,
		to: account,
		reverse: false,
		limit: session.unread,
		done: function(error, obj) {
			if(!error) {
				var msgs = obj.msgs
				for(var j = 0; j < msgs.length; j++) {
					if(null!==msgs[j].apns && null!==msgs[j].apns.accounts && msgs[j].apns.accounts.contains(that.accid)) {
						as++
					}
				}
				if(as > 0) {
					var panel_item = $("li[data-account='" + session.id.split("-")[1] + "']");
					if(null != panel_item.find(".panel_multi-row .panel_been_a")) {
						panel_item.find(".panel_multi-row .panel_been_a").remove()
					}
					panel_item.find(".panel_multi-row").eq(1).append("<span class=\"panel_been_a\">@" + (as == 1 ? "" : as) + "</span>");
				} else {
					if(null != panel_item.find(".panel_multi-row .panel_been_a")) {
						panel_item.find(".panel_multi-row .panel_been_a").remove()
					}
				}
			}
		}
	}
	this.mysdk.getHistoryMsgs(param)
}*/